package com.atguigu.gmall.product.controller.admin;

import com.atguigu.gmall.common.result.Result;
import com.atguigu.gmall.model.product.*;
import com.atguigu.gmall.product.service.BaseManageService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.Future;

@RestController
@RequestMapping("/admin/product")
public class AdminBaseManageController {

    @Autowired
    private BaseManageService baseManageService;

    /**
     * 查询一级分类
     * @return
     */
    @GetMapping("/getCategory1")
    public Result<List<BaseCategory1>> getCategory1() {
        List<BaseCategory1> baseCategory1List = baseManageService.getBaseCategory1List();
        return Result.ok(baseCategory1List);
    }

    /**
     * 查询一级分类下的二级分类
     * @param category1Id
     * @return
     */
    @GetMapping("/getCategory2/{category1Id}")
    public Result<List<BaseCategory2>> getCategory2(@PathVariable("category1Id") Long category1Id) {
        List<BaseCategory2> baseCategory2List = baseManageService.getCategory2List(category1Id);
        return Result.ok(baseCategory2List);
    }

    /**
     * 查询二级分类下的三级分类
     * @param category2Id
     * @return
     */
    @GetMapping("/getCategory3/{category2Id}")
    public Result<List<BaseCategory3>> getCategory3(
            @PathVariable("category2Id") Long category2Id) {
            List<BaseCategory3> baseCategory3List = baseManageService.getCategory3List(category2Id);
        return Result.ok(baseCategory3List);
    }

    /**
     * 查询各级分类下的平台属性
     * @param category1Id
     * @param category2Id
     * @param category3Id
     * @return
     */
    @GetMapping("/attrInfoList/{category1Id}/{category2Id}/{category3Id}")
    public Result<List<BaseAttrInfo>> getBaseAttrInfoWithValue(
            @PathVariable("category1Id") Long category1Id,
            @PathVariable("category2Id") Long category2Id,
            @PathVariable("category3Id") Long category3Id) {
        List<BaseAttrInfo> attrInfoList = baseManageService.getBaseAttrInfoList(category1Id, category2Id, category3Id);
        return Result.ok(attrInfoList);
    }

    /**
     * 保存平台属性以及平台属性值
     * @param baseAttrInfo
     * @return
     */
    @PostMapping("/saveAttrInfo")
    public Result<Void> saveAttrInfo(@RequestBody BaseAttrInfo baseAttrInfo) {
        baseManageService.saveAttrInfo(baseAttrInfo);
        return Result.ok();
    }

    /**
     *获取平台属性值
     * @param attrId
     * @return
     */
    @GetMapping("/getAttrValueList/{attrId}")
    public Result<List<BaseAttrValue>> getAttrValueList(@PathVariable Long attrId) {
        List<BaseAttrValue> baseAttrValueList = baseManageService.getBaseAttrValueList(attrId);
        if (baseAttrValueList == null) {
            Result<List<BaseAttrValue>> result = Result.fail();
            result.setMessage("平台属性不存在，无法查询平台属性值");
            return result;
        }
        return Result.ok(baseAttrValueList);
    }

}
